//
//  NSString+CPString.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/5.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "NSString+CPString.h"

@implementation NSString (CPString)

- (CGSize)cp_height:(CGFloat)size
{
    UIFont *font = [UIFont systemFontOfSize:size];
    CGSize sizeWord = [@"一" sizeWithFont:font constrainedToSize:CGSizeMake(100, 1000.0) lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat width = sizeWord.width;//一个汉字的宽度
    
    CGSize sizeStr = [self sizeWithFont:font constrainedToSize:CGSizeMake(width, 1000.0) lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat hight = sizeStr.height;
    
    return CGSizeMake(width+6, hight+6);
}

- (NSString *)cp_author:(NSString *)author dynasty:(NSString *)dynasty title:(NSString *)title
{
    NSString *ct = [self stringByReplacingOccurrencesOfString:@"　　" withString:@""];
    NSString *ct1 = [ct stringByReplacingOccurrencesOfString:@"！" withString:@"\n"];
    NSString *ct0 = [ct1 stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *c = [ct0 stringByReplacingOccurrencesOfString:@"？" withString:@"\n"];
    NSString *c1 = [c stringByReplacingOccurrencesOfString:@"，" withString:@"\n"];
    NSString *c2 = [c1 stringByReplacingOccurrencesOfString:@"。" withString:@"\n"];
    NSString *lastStr = [NSString stringWithFormat:@"%@\n%@·%@\n%@", title, dynasty, author, c2];
    return lastStr;
}

- (NSString *)cp_replaceString
{
    NSString *c = [self stringByReplacingOccurrencesOfString:@"　　" withString:@""];
    NSString *c0 = [c stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *c1 = [c0 stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
    NSString *c2 = [c1 stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"];
    NSString *c3 = [c2 stringByReplacingOccurrencesOfString:@"<p>" withString:@"\n"];
    NSString *c4 = [c3 stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];
    NSString *c5 = [c4 stringByReplacingOccurrencesOfString:@"<strong>" withString:@""];
    NSString *c6 = [c5 stringByReplacingOccurrencesOfString:@"</strong>" withString:@""];
    NSString *c7 = [c6 stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
    NSString *c8 = [c7 stringByReplacingOccurrencesOfString:@"<p align=\"center\">" withString:@""];
    NSString *replace = [c8 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *new = replace.length >0 ? [replace substringFromIndex:1] : @"";
    return new;
}

- (CGSize)cp_sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (CGSize)cp_sizeWithFont:(UIFont *)font
{
    return [self cp_sizeWithFont:font maxW:MAXFLOAT];
}

+ (NSString *)cp_tsp
{
    NSTimeInterval tmp =[[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970]*1000;
    return [[NSString stringWithFormat:@"%f", tmp] componentsSeparatedByString:@"."].firstObject;
}

+ (NSString *)cp_tsp:(NSString *)tsp
{
    // @"1970-01-01T00:00:00Z"
    NSTimeInterval _interval=[tsp doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
    return [objDateformat stringFromDate: date];
}
@end
